import { toJS } from 'mobx';
import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';
import { WeaTools } from 'ecCom';
import { Modal, message } from 'antd';
import * as Apis from '../apis/main';

import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class CrmPortalStore {
    @observable initResponsed = false;// 是否已经得到初始化数据的响应
    @observable initSuccessed = false;// 初始化数据是否获取成功
    @observable initFailedMsg = ''; // 初始化处理失败的原因
    @observable topMenus = [];
    @observable topMenuSelected = {};
    @observable quickSearchTypesVisible = false;
    @observable quickSearchTypes = [];
    @observable quickSearchTypesSelected = {};
    @observable topMenu1Children = [];
    @observable topMenu2Children = [];
    @observable topMenu3Children = [];
    @observable topMenu4Children = [];
    @observable topMenu5Children = [];
    @observable topMenu6Children = [];
    @observable leftMenu = [];
    @observable leftMenuSelected = '1';
    @observable leftMenuType = 'portal';
    @observable leftMenuMode = 'inline';
    @observable leftMenuVisible = true;
    @observable logoInfo = '';
    @observable welcomeInfo = '';

    @action
    doInit=(params = {}) => {
      Apis.getPortalInitData(params).then((data) => {
        this.doInitCallBack(params, data);
      });
    }

    @action
    doInitCallBack=(params, data) => {
      this.initResponsed = true;
      if (data.status == 'success') {
        document.title = data.htmlTitle;
        this.initSuccessed = true;
        this.logoInfo = data.logoInfo;
        this.welcomeInfo = data.welcomeInfo;
        this.topMenus = data.topMenus;
        this.topMenuSelected = data.topMenuSelected;
        this.quickSearchTypes = data.quickSearchTypes;
        this.quickSearchTypesSelected = data.quickSearchTypesSelected;
        this.topMenu1Children = data.topMenu1Children;
        this.topMenu2Children = data.topMenu2Children;
        this.topMenu3Children = data.topMenu3Children;
        this.topMenu4Children = data.topMenu4Children;
        this.topMenu5Children = data.topMenu5Children;
        this.topMenu6Children = data.topMenu6Children;
        this.changeTopMenuSelected(this.topMenuSelected);
      } else {
        this.initSuccessed = false;
        this.initFailedMsg = data.msgcode;
      }
    }

    @action
    doClear = () => {
      this.initResponsed = false;
      this.initSuccessed = false;
      this.initFailedMsg = '';
    }

    @action
    changeQuickSearchTypesVisible(visible) {
      this.quickSearchTypesVisible = visible;
    }

    @action
    changeQuickSearchTypesSelected(quickSearchTypesSelected) {
      this.quickSearchTypesSelected = quickSearchTypesSelected;
      this.changeQuickSearchTypesVisible(false);
    }

    @action
    changeTopMenuSelected(topMenuSelected) {
      this.topMenuSelected = topMenuSelected;
      const type = topMenuSelected.type.toString();
      if (type == '1') {
        this.leftMenu = this.topMenu1Children;
      } else if (type == '2') {
        this.leftMenu = this.topMenu2Children;
      } else if (type == '3') {
        this.leftMenu = this.topMenu3Children;
      } else if (type == '4') {
        this.leftMenu = this.topMenu4Children;
      } else if (type == '5') {
        this.leftMenu = this.topMenu5Children;
      } else if (type == '6') {
        this.leftMenu = this.topMenu6Children;
      }
      this.leftMenuSelected = '1';
      const obj = this.getSelectedLeftMenu();
      this.onLoadMain(obj);
    }

    @action
    getSelectedLeftMenu() {
      for (const lm of this.leftMenu) {
        if (lm.id == this.leftMenuSelected) {
          return lm;
        }
      }
    }

    @action
    changeLeftMenuSelected(leftMenuSelected) {
      this.leftMenuSelected = leftMenuSelected;
    }

    @action
    changeLeftMenuMode(leftMenuMode) {
      this.leftMenuMode = leftMenuMode;
    }

    @action
    onLoadMain(obj) {
      weaHistory.push({
        pathname: obj.url,
      });
    }

    @action
    onQuickSearch(v) {
      const type = toJS(this.quickSearchTypesSelected).type;
      const obj = {};
      if (type == 3) {
        obj.url = `/main/crm/customer/customerSearchResult?fromPortal=true&name=${v}`;
      } else if (type == 5) {
        obj.url = `/main/workflow/queryFlowResult?from=quickSearch&fromwhere=urlFilter&requestname=${v}`;
      } else if (type == 1) {
        obj.url = `/main/document/search?docsubject=${v}`;
      } else if (type == 6) {
        obj.url = `/main/prj/queryProjectResult?name=${v}`;
      }
      this.onLoadMain(obj);
    }

    @action
    onPasswordClick() {
      const obj = { url: '/main/changePassword' };
      this.onLoadMain(obj);
    }

    @action
    onLogout() {
      Modal.confirm({
        title: getLabel(15172, '系统提示'),
        content: getLabel(16628, '确定要退出系统吗？'),
        okText: getLabel(33703, '确定'),
        cancelText: getLabel(201, '取消'),
        onOk: () => {
          Apis.logout({}).then((result) => {
            WeaTools.ssoLogout(result, false);
            window.location.href = '/wui/index.html#/?logintype=2';
          });
        },
      });
    }
}
